Public memoria As String
Public borrar As Boolean
Public total As Boolean
Public haypunto As Boolean
Public coma As String
Public precision As Integer
Const maxPrecision As Integer = 8

Dim op, opant As Integer

Private Sub b0_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    mostrar(0)
End Sub

Private Sub b1_Click()
    mostrar(1)
End Sub

Private Sub b2_Click()
    mostrar(2)
End Sub

Private Sub b3_Click()
    mostrar(3)
End Sub

Private Sub b4_Click()
    mostrar(4)
End Sub

Private Sub b5_Click()
    mostrar(5)
End Sub

Private Sub b6_Click()
    mostrar(6)
End Sub

Private Sub b7_Click()
    mostrar(7)
End Sub
Private Sub b8_Click()
    mostrar(8)
End Sub

Private Sub b9_Click()
    mostrar(9)
End Sub

Private Sub b0_Click()
    mostrar(0)
End Sub

Private Sub bp_Click()
    If InStr(tbRes.Text, coma) <> 0 And borrar = False Then haypunto = True Else haypunto = False
    If Not (haypunto) Then
        mostrar(coma)
    End If
End Sub

Private Sub bsum_Click()
    sumar()
End Sub

Private Sub bres_Click()
    restar()
End Sub

Private Sub bmul_Click()
    multiplicar()
End Sub

Private Sub bdiv_Click()
    dividir()
End Sub

Private Sub bigual_Click()
    igual()
End Sub

Sub igual()
    If memoria <> "" Then total = True
    Call opera(memoria, tbRes.Text)
    memoria = ""
    op = 0
    btRet.Enabled = False
    agregar_Panel(memoria)
End Sub

Sub sumar()
    Call opera(memoria, tbRes.Text)
    op = 1
End Sub

Sub restar()
    Call opera(memoria, tbRes.Text)
    op = 2
End Sub

Sub multiplicar()
    Call opera(memoria, tbRes.Text)
    op = 3
End Sub

Sub dividir()
    Call opera(memoria, tbRes.Text)
    op = 4
End Sub

Sub limpiar_ultimo()
    tbRes.Text = "0"
End Sub

Sub limpiar_todo()
    tbRes.Text = "0"
    memoria = ""
    borrar = True
    tbPanel.Text = ""
End Sub

Sub opera(val1 As String, val2 As String)
    opant = op
    If memoria = "" Then
        memoria = val2
    Else
        Select Case opant
            Case 1 'Suma
                If memoria = "" Then
                    memoria = val2
                Else
                    memoria = Round(CDbl(val1) + CDbl(val2), precision)
                    agregar_Panel("+ " & val2)
                End If
            Case 2 ' Resta
                If memoria = "" Then
                    memoria = val2
                Else
                    memoria = Round(CDbl(val1) - CDbl(val2), precision)
                    agregar_Panel("- " & val2)
                End If
            Case 3 ' Multiplicacin
                If memoria = "" Then
                    memoria = val2
                Else
                    memoria = Round(CDbl(val1) * CDbl(val2), precision)
                    agregar_Panel("* " & val2)
                End If
            Case 4 ' Divisin
                If memoria = "" Then
                    memoria = val2
                Else
                    If CDbl(val2) <> 0 Then
                        memoria = Round(CDbl(val1) / CDbl(val2), precision)
                    Else
                        memoria = 0
                        agregar_Panel("ERROR")
                    End If
                    agregar_Panel("/ " & val2)
                End If
        End Select
    End If
    tbRes.Text = memoria
    If total Then
        agregar_Panel("-----------")
    End If
    agregar_Panel(memoria)
    borrar = True
    btRet.Enabled = True
    total = False
End Sub

Sub agregar_Panel(s As String)
    If tbPanel.Text <> "" And s <> "" Then
        tbPanel.Text = tbPanel.Text & vbCrLf
    End If
    tbPanel.Text = tbPanel.Text & s
End Sub

Private Sub btnC_Click()
    limpiar_ultimo()
End Sub

Private Sub btnAC_Click()
    limpiar_todo()
End Sub

Sub retroceso()
    Dim s As String
    s = tbRes.Text
    If s <> "" And s <> "0" And Len(s) > 1 Then
        s = Left(s, Len(s) - 1)
    Else
        s = "0"
        btRet.Enabled = False
    End If
    tbRes.Text = s
End Sub

Sub mas_precision()
    precision = precision + 1
    If precision > maxPrecision Then precision = maxPrecision
    lbPrecision.Caption = precision
    tbPanel.SetFocus()
End Sub

Sub menos_precision()
    precision = precision - 1
    If precision < 0 Then precision = 0
    lbPrecision.Caption = precision
    tbPanel.SetFocus()
End Sub

Private Sub btnOff_Click()
    Unload(Me)
End Sub

Sub mostrar(c As String)
    If borrar Then
        borrar = False
        tbRes.Text = ""
    End If
    If tbRes.Text = "0" And c <> coma Then tbRes.Text = ""
    If tbRes.Text = coma Then tbRes.Text = "0" & coma
    tbRes.Text = tbRes.Text & c
    btRet.Enabled = True
End Sub

Private Sub btnPr0_Click()
    menos_precision()
End Sub

Private Sub btnPr1_Click()
    mas_precision()
End Sub

Private Sub btnPrint_Click()
    imprimir(tbPanel.Text)
    tbRes.SetFocus()
End Sub

Private Sub btnSalir_Click()
    Unload(Me)
End Sub

Private Sub btRet_Click()
    retroceso()
End Sub

Private Sub Image1_Click()
    Unload(Me)
End Sub

Private Sub tbPanel_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode >= 96 And KeyCode <= 105 Then
        mostrar(KeyCode - 96)
    End If
    If KeyCode = 110 Then
        mostrar(coma)
    End If
    Select Case KeyCode
        Case 107 ' Sumar
            sumar()
        Case 109 ' Restar
            restar()
        Case 106 ' Multiplicar
            multiplicar()
        Case 111 ' Dividir
            dividir()
        Case 48 ' Igual
            igual()
        Case 13
            igual()
        Case 65 ' Limpiar todo (A)
            limpiar_todo()
        Case 67 ' limpiar ultimo (C)
            limpiar_ultimo()
        Case 8 ' Retroceso
            retroceso()
        Case 226 ' Menos precision
            If Shift = 0 Then
                menos_precision()
            Else
                mas_precision()
            End If
        Case 70 ' Fin (F)
            Unload(Me)
        Case 80 ' Print (P)
            imprimir(tbPanel.Text)
    End Select

End Sub

Sub imprimir(ByVal texto As String)
    Dim existe As Boolean
    Dim h As Worksheet
    For Each h In ActiveWorkbook.Worksheets
        If h.Name = "work" Then existe = True
    Next
    If Not (existe) Then
        ActiveWorkbook.Sheets.Add(Before:=Worksheets(Worksheets.Count))
        ActiveWorkbook.Sheets(Worksheets.Count).Name = "work"
    End If
    Worksheets("work").Select()
    Worksheets("work").UsedRange.Delete()
    Dim lineas As Object
    lineas = Split(texto, Constants.vbNewLine)
    Dim i As Integer
    For i = 0 To UBound(lineas)
        Debug.Print(lineas(i))
        Cells(i + 1, 1) = "'" & lineas(i)
    Next
    Worksheets("work").Columns("A").AutoFit()
    Worksheets("work").Columns("A").HorizontalAlignment = xlRight
    Range(Cells(1, 1), Cells(i, 1)).BorderAround(LineStyle:=xlContinuous, Weight:=xlThin, Color:=5)
    Range(Cells(i, 1), Cells(i, 1)).Interior.ColorIndex = 6
    Range("A1:A6").Select()
    Range(Cells(1, 1), Cells(i, 1)).NumberFormat = "@"
    Range(Cells(1, 1), Cells(i, 1)).PrintOut()
    Worksheets("work").UsedRange.Delete()
    Worksheets("work").Range("A1").Select()
End Sub

Private Sub UserForm_Initialize()
    memoria = ""
    borrar = False
    tbRes.Text = "0"
    btRet.Enabled = False
    total = False
    tbPanel.ScrollBars = fmScrollBarsBoth
    tbPanel.SetFocus()
    Dim textNumero As Double
    TestNumero = Format("0.1", "#. #")
    If InStr(TestNumero, ",") Then coma = "," Else coma = "."
    precision = 4
    lbPrecision.Caption = precision
End Sub

